//
//  HXNavController.m
//  爱鲜蜂
//
//  Created by 洪东恒 on 16/4/3.
//  Copyright © 2016年 建国. All rights reserved.
//
#import "HXNavController.h"

@interface HXNavController ()

@end

@implementation HXNavController

+(void)load
{
    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[self]];    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    // boldSystemFontOfSize:20  粗体
    dict[NSFontAttributeName] = [UIFont boldSystemFontOfSize:17];
    
    dict[NSForegroundColorAttributeName] = [UIColor blackColor];

    navBar.titleTextAttributes = dict;
    
    navBar.tintColor = [UIColor redColor];
    
    //设置导航条背景颜色
    [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
   
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    //住控制器不需要 返回按钮   所以要判断下
    //self.childViewControllers.count > 0  当子控制器个数 大于0的时候说明有根控制器
    if (self.childViewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES ;
        //设置一个按钮替换系统的left
        UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
        //设置图片
        [but setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [but setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        //设有文字
//        [but setTitle:@"返回" forState:UIControlStateNormal];
        [but setImage:[UIImage imageNamed:@"v2_goback"] forState:(UIControlStateNormal)];
        //设置文字颜色
//        [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //设置尺寸
        [but sizeToFit];
        //设置按钮偏移量
        but.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
        //设置监听的方法
        [but addTarget:self action:@selector(clickBut) forControlEvents:UIControlEventTouchUpInside];
        //添加到导航控制器中
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:but];
    }
    //调用系统的方法
    [super pushViewController:viewController animated:animated];
}

//监听方法
-(void)clickBut
{
    
    //返回上一个界面
    [self popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //如果不设置根控制器的下不触发手势 会发生假死的现象
    
    //设置平移手势  添加到view上  代理和代理的方法  使用系统的
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    //添加手势
    [self.view addGestureRecognizer:pan];
    
    //设置手势控制器的代理  什么时候触发
    pan.delegate = self;
    
    //实现系统的滑动返回功能
    //清空手势代理   这样取消了系统的滑动返回按钮   也可以有滑动返回的功能
    //    self.interactivePopGestureRecognizer.delegate = self;
    // 清空手势代理,恢复滑动返回功能
    self.interactivePopGestureRecognizer.enabled = NO;
    
}

//设置在根控制器下  手势不起作用
// 是否触发手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    
    //判断是否是根控制器
    return self.childViewControllers.count > 1;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
